<?php
	class Admincp_Model_User extends Zend_Db_Table_Abstract
	{
		protected $_name = 'user';
		protected $_primary = 'id';
		protected $_db;
		
		public function __construct()
		{
			$this->_db = Zend_Registry::get('db');
		}
		
		public function listUser()
		{
		    $data = array(
		        'id',
		        'fullname',
		        'email',
                'avatar',
                'gender',
                'birthday',
                'role',
		        'status',
		    );
		    $sql = $this->_db->select()
                            ->from($this->_name, $data)
                            ->order('id DESC');
		    return $sql;
		}
		
		public function createUser($data)
		{
		    $this->_db->insert($this->_name, $data);
		}
		
		public function updateUser($data, $id)
		{
		    $id = (int) $id;
		    $where = $this->getAdapter()->quoteInto('id = ?', $id);
		    $this->_db->update($this->_name, $data, $where);
		}
		
		public function deleteUser($id)
		{
		    $id = (int) $id;
		    $where = $this->getAdapter()->quoteInto('id = ?', $id);
            $avatar = $this->getAvatarById($id);
            if(is_file(PUBLIC_PATH . '/files/user/' . $avatar)) {
                @unlink(PUBLIC_PATH . '/files/user/' . $avatar);
            }
		    $this->_db->delete($this->_name, $where);
		}
		
		public function deleteMultiUser($listId)
		{
		    foreach($listId as $value) {
		        $this->deleteUser($value);
		    }
		}
        
        public function getAvatarById($id)
        {
            $sql = $this->_db->select()
                            ->from($this->_name, array('avatar'))
                            ->where('id = ?', $id);
            $result = $this->_db->fetchOne($sql);
            return $result;
        }
		
		public function getUserById($id)
		{
		    $id = (int) $id;
            $data = array(
                'fullname',
		        'email',
                'avatar',
                'gender',
                'birthday',
                'role',
		        'status',
            );
		    $sql = $this->_db->select()
		                    ->from($this->_name, $data)
		                    ->where('id = ?', $id);
		    $result = $this->_db->fetchRow($sql);
		    return $result;
		}
		
		public function getUserByEmail($email)
		{
		    $sql = $this->_db->select()
		                    ->from($this->_name)
		                    ->where('email = ?', $email);
		    $result = $this->_db->fetchRow($sql);
		    return $result;
		}
		
		public function checkUser($id = null, $email)
		{
		    $sql = $this->_db->select()
		                    ->from($this->_name)
		                    ->where('email = ?', $email);
		    if($id != null) {
		        $sql = $sql->where('id <> ?', $id);
		    }
		    $result = $this->_db->fetchAll($sql);
		    return count($result);
		}
		
		public function checkCode($id, $code)
		{
		    $sql = $this->_db->select()
		                    ->from($this->_name)
		                    ->where('id = ?', $id)
		                    ->where('code = ?', $code);
		    $result = $this->_db->fetchAll($sql);
		    return $result;
		}
		
		public function checkPassword($id, $password)
		{
		    $sql = $this->_db->select()
		                    ->from($this->_name)
		                    ->where('id = ?', $id)
		                    ->where('password = ?', $password);
		    $result = $this->_db->fetchAll($sql);
		    return count($result);
		}
        
        public function getRoleById($id)
        {
            $sql = $this->_db->select()
                            ->from('user_permission', array('name'))
                            ->where('id = ?',$id);
            $result = $this->_db->fetchOne($sql);
            return $result;
        }
		
		public function searchUser($data)
		{
		    $user = array(
		        'id',
		        'fullname',
		        'email',
                'avatar',
                'gender',
                'birthday',
                'role',
		        'status',
		    );
		    $sql = $this->_db->select()
		                    ->from($this->_name, $user);
		    if($data['fullname'] != '') {
		        $sql = $sql->where('fullname_plaintext LIKE ?', $data['fullname']);
		    }
		    if($data['email'] != '') {
		        $sql = $sql->where('email LIKE ?', $data['email']);
		    }
            if($data['gender'] != 0) {
		        $sql = $sql->where('gender = ?', $data['gender']);
		    }
		    if($data['status'] != 0) {
		        $sql = $sql->where('status = ?', $data['status']);
		    }
		    return $sql;
		}
	}
